<?php

require_once "services/FacebookProxy.php";
require_once "www/_include/smarty/libs/Smarty.class.php";
require_once "data/dao/GameDAO.php";
require_once "data/dao/WishListDAO.php";
require_once "data/dao/CollectionDAO.php";
require_once "application/game/Game.php";
require_once "application/game/CountedGame.php";
require_once "utils/ClassUtils.php";

class TopGamesRenderer{
	
	private $smarty;
	private $facebook;
	
	public function TopGamesRenderer(){
		
		$this->smarty = new Smarty();
		$this->facebook = new FacebookProxy();
		$this->facebook->requireFrame();
		$this->facebook->requireLogin();
				
		$this->getPlaceholderValues();
		$this->renderPage();
	}	
	
	private function getPlaceholderValues(){
		
		$this->smarty->assign("styleSheet", file_get_contents("html/wiiSocial.css"));
		$this->smarty->assign("mostWantedGames", $this->getMostWantedGames());
		$this->smarty->assign("mostOwnedGames", $this->getMostOwnedGames());
		$this->smarty->assign("HOST", HOST_URL);
	}
	
	private function renderPage(){
		$this->smarty->display("topGames.tpl");
	}
	
	/**
	 * returns an array of 10 TopGame objects.
	 * The objects are most wanted games
	 * 
	 * @return array
	 */
	private function getMostWantedGames(){
		
		$gdao = new GameDAO();
		$wldao = new WishListDAO();
		$games = $gdao->getAllGames();
		
		$topGames = array();
		
		foreach($games as $g){
			array_push($topGames, new TopGame($g->getID(), $wldao->getWishListCount($g->getID())));
		}
		
		$topGames = TopGame::getTop10ByCount($topGames);
		
		$sortedTopGames = array();
		foreach ($topGames as $g){
			$game = ClassUtils::typecast($gdao->getGame($g->getGameId()), "CountedGame");
			$game->setCount($g->getCount());
			array_push($sortedTopGames, $game);
		}
		return $sortedTopGames;
	}
	
	private function getMostOwnedGames(){
		
		$gdao = new GameDAO();
		$cdao = new CollectionDAO();
		$games = $gdao->getAllGames();
		
		$topGames = array();
		
		foreach($games as $g){
			array_push($topGames, new TopGame($g->getID(), $cdao->getCollectionCount($g->getID())));
		}
		
		$topGames = TopGame::getTop10ByCount($topGames);
		
		$sortedTopGames = array();
		foreach ($topGames as $g){
			$game = ClassUtils::typecast($gdao->getGame($g->getGameId()), "CountedGame");
			$game->setCount($g->getCount());
			array_push($sortedTopGames, $game);
		}
		return $sortedTopGames;
	}
	
}
$tgr = new TopGamesRenderer();

class TopGame{
	
	private $gameId;
	private $count;
	
	public function TopGame($gameId, $count){
		
		$this->gameId = $gameId;
		$this->count = $count;
	}
	
	public function getGameId(){
		return $this->gameId;
	}
	
	public function getCount(){
		return $this->count;
	}
	
	/**
	 * sorts an array of TopGame objects by count,
	 * using the bubble sort algorithm.
	 * 
	 * @param array of TopGame objects
	 * @return array of TopGames sorted by count
	 */
	public static function getTop10ByCount(&$data){
		
		$tmp = "";
		$size = count($data);
    	for ($i = 0; $i< $size-1; $i++){
    		
      		for ($j = 0; $j < $size-1-$i; $j++){
      			
           		if ($data[$j+1]->getCount() < $data[$j]->getCount()){
                	$tmp = $data[$j];
                	$data[$j] = $data[$j+1];       
                	$data[$j+1] = $tmp;
           		}
      		}
    	}
    	$desc = array_reverse($data); 
    	return array_splice($desc, 0, 10);
	}
}
?>
